package Presentation.TextualUI;

import BusinessLogic.ControllerFacade;
import Presentation.UserInterface;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Collection;

public class TextualUI extends UserInterface {

    private void display(String str) {
        System.out.println(str);
    }

    public void run() {
        ControllerFacade c = m_controllerFacade;
        display("Movie System initiated.");

        String command = "";
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);

        while (! command.equals("exit")){
            try{
                command = reader.readLine();
                String[] cmd = command.split(" ");
                if (cmd[0].equals("login"))
                    try {
                        c.login(cmd[1], cmd[2]);
                        display("user "+cmd[1]+" has been logged in");
                    }
                    catch (Exception e) {
                        display(e.getMessage());
                    }
                else if (cmd[0].equals("logout"))
                    try {
                        c.logout();
                        display("user has been logged out");
                    }
                    catch (Exception e) {
                        display(e.getMessage());
                    }
                else if (cmd[0].equals("movies")) {
                    try {
                        Map<Double,String> t = c.getRecommendation(0);
                        for(Map.Entry m : t.entrySet()) {
                            display("Movie: "+m.getKey()+"\t"+m.getValue());
                        }
                    }
                    catch (Exception e){
                        display(e.getMessage());
                    }
                }
                else if (cmd[0].equals("?")){
                    display("\tlogin username password");
                    display("\tlogout username");
                    display("\tmovies username");
                    display("\tload dataFile");
                    display("\tsave dataFile");
                    display("\texit");
                }
                else if (cmd[0].equals("UsersRatedLastMonth")){
                        int count = Integer.parseInt(cmd[1]);
                        Collection<String> list=   c.getUsersRatedLastMonth(count,0);
                        for (String s: list)
                           display(s);

                }
                    else
                    display("Unknown command");
            }
            catch(Exception e){
                display("Command Error! exiting..."+e.getMessage());
                e.printStackTrace();
                command = "exit";
            }
        }
        
        display("Movie System terminated.");
    }
}
